/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package brutelistcreator;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

/**
 *
 * @author Adrian Eckardt
 */
public class Calc implements Runnable{
    
     
    //INIT VARS
    public static String numbers = "0123456789";
    public static String lowerChar = "abcdefghijklmnopqrstuvwxyz";
    public static String upperChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static String specialChar = "";
    public static String processCnt = "";
    
    public String sz, TextLength, out;
    public boolean num, bs_lower, bs_upper, add;
    

    @Override
    public void run(){
    
        specialChar = sz;
        double  Count = 0;
        
        int tmpLengthInt = Integer.parseInt(TextLength);
        
        String tmpCharset = "";
        
        if(num == true)
        {
            tmpCharset+=numbers;
        }
        if(bs_lower == true)
        {
            tmpCharset+=lowerChar;
        }
        if(bs_upper == true)
        {
            tmpCharset+=upperChar;
        }
        tmpCharset+=specialChar;

        
        int charsetLength = tmpCharset.length();
        double ausgText = Math.pow(charsetLength, tmpLengthInt);        
        
        
        String letters = tmpCharset;
        int num = tmpLengthInt;
        String last = repeat(String.valueOf(letters.charAt(0)), num);

        
        //DATEIHANDLE
        boolean anfuegen = false;
        
        if(add == true)
        {
            anfuegen = true;
        }
        String filename = "Wordlist.txt";
        
        String TmpName = out;
        
        if(TmpName.length()>=1)
        {
            filename = TmpName;
        }
        
        try {
            //CREATE-OPEN FILE
            BufferedWriter output = new BufferedWriter(new FileWriter(filename, anfuegen));
            output.write(last + "\r\n");
            while(!last.equals(repeat(String.valueOf(letters.charAt(letters.length()-1)), num)))
            { 
                //GET CHARS AND WRITE TO FILE 
                last = char_add(letters,last,num-1); 
                output.write(last + "\r\n");
                Count++;
                processCnt = String.valueOf(Math.round((Count*100)/ausgText));
                brutelistcreator.BruteListCreatorView.statusBar.setValue((int)Math.round((Count*100)/ausgText));
            }
            output.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
        //ENABLE BUTTONS
        brutelistcreator.BruteListCreatorView.B_CREATE.setEnabled(true);
        brutelistcreator.BruteListCreatorView.B_COUNT.setEnabled(true);
        Count=0;
        
    }

    
    public static String char_add(String digits, String str, int cha){ 
    
        if(cha >=0 && cha <= str.length()-1)
        {
            String first = String.valueOf(str.charAt(cha));
            String akt = String.valueOf(digits.charAt(digits.length()-1));

            if(first.equals(akt))
            {
                str = changeall(str,digits.charAt(0),cha); 
                return char_add(digits,str,cha-1);
            }
            else
            {
                StringBuilder sb = new StringBuilder(str);
                if(digits.indexOf(first)+1 <= digits.length()-1)
                {
                      sb.setCharAt(cha, digits.charAt(digits.indexOf(first)+1));
                      return String.valueOf(sb);
                }
                else
                {
                    return str;
                }
            }
        }
        else
        {
            return str;
        }
    }
    
    public static String changeall(String str, char cha, int start){
        

        int  en = str.length()-1;
        StringBuilder sb = new StringBuilder(str);
        for(int i=start;i<=en;i++){
            sb.setCharAt(i, cha);
        }
        return String.valueOf(sb); 
    } 
    
    public static String repeat(String s, int n) {
        if (s == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            sb.append(s);
        }
        return sb.toString();
    }

}
